const Zan = require('../db').Zan
const Commit = require('../db').Commit

module.exports = {
    async addZan(ctx, next){
        let { type_id='' , type = '' } = ctx.request.body;
        if (!type_id && !type) {
            ctx.body = {
                code: 500,
                msg: '服务器异常，请稍后再试!'
            }
            return 
        }
        try {
            let zanres = await Zan.find({user_id:ctx._id,type_id})
            if (zanres.length !== 0) {
                ctx.body = {
                    code: 501,
                    msg: '您已经赞过了 不要重新点赞!'
                }
                return 
            }
            let newZan = new Zan({
                type_id,
                type,
                user_id:ctx._id,
                status:1
            })
            await newZan.save()
            await Commit.findOneAndUpdate({_id: type_id}, { $inc: { 'like': 1 } })
            ctx.body = {
                code: 200,
                msg: '成功!'
            }
        }catch (e) {
            ctx.body = {
                code: 500,
                msg: '服务器异常，请稍后再试!'
            }
        }
    }
}